//全选
function selectAll() {
	var selectall = document.getElementById("selectall");
	var inputs = document.getElementsByTagName("input");
	for(var i = 0; i < inputs.length; i++) {
		if(inputs[i].type == "checkbox") {
			inputs[i].checked = selectall.checked;
		}
	}
	totalPrice()
	points()
}

//删除所选
function delSelect() {
	commoditys = document.getElementById("shopping_car").children[1].getElementsByTagName("input");
	if(confirm("确认删除所选商品吗？")) {
		for(var i = commoditys.length - 1; i >= 0; i--) {
			if(commoditys[i].checked == "1") {
				var commodity = commoditys[i].parentNode.parentNode;
				var store = commodity.previousElementSibling;
				document.getElementById("shopping_car").children[1].removeChild(commodity);
				document.getElementById("shopping_car").children[1].removeChild(store);
				totalPrice();
				points();
			}
		}
	}
}

//删除当前行商品
function delCommodity(t) {
	var commodity = t.parentNode.parentNode;
	var store = commodity.previousElementSibling;
	if(confirm("确认删除该商品吗？")) {
		document.getElementById("shopping_car").children[1].removeChild(commodity);
		document.getElementById("shopping_car").children[1].removeChild(store);
	}
	totalPrice();
	points()
}

//商品增加
function addCommodity(t) {
	var quantity = t.parentNode.children[1].value;
	++quantity;
	t.parentNode.children[1].value = quantity;
	subPrice(t);
	totalPrice();
	points();
}

//商品减少
function minusCommodity(t) {
	var quantity = t.parentNode.children[1].value;
	--quantity;
	if(quantity > 0) {
		t.parentNode.children[1].value = quantity;
		subPrice(t);
		totalPrice();
		points();
	}
}

//小计
function subPrice(t) {
	var commodity = t.parentNode.parentNode;
	var unitprice = commodity.children[3].innerHTML;
	var quantity = commodity.children[4].children[1].value;
	var subtotal = unitprice * quantity;
	commodity.children[5].innerHTML = subtotal;
}

//总价
function totalPrice() {
	var totalp = 0;
	var totalprice = document.getElementById("totalprice");
	var subtotals = document.getElementById("shopping_car").children[1].children;
	for(var i = 0; i < subtotals.length; i++) {
		if(subtotals[i].children.length > 1) {
			if(subtotals[i].children[0].children[0].checked == true) {
				totalp += parseInt(subtotals[i].children[5].innerHTML);
			}
		}
	}
	totalprice.innerHTML = totalp;
}

//积分
function points() {
	var points = 0;
	commodity = document.getElementById("shopping_car").children[1].children;
	for(var i = 0; i < commodity.length; i++) {
		if(commodity[i].children.length > 1) {
			if(commodity[i].children[0].children[0].checked == true) {
				var point = commodity[i].children[2].innerHTML;
				var quantity = commodity[i].children[4].children[1].value;
				points += parseInt(point) * parseInt(quantity);
			}
		}
	}
	document.getElementById("points").innerHTML = points;
}

//添加新的商品
function addNew() {
	var shoppingcar = document.getElementById("shopping_car");
	var store = document.createElement("tr");
	store.innerHTML = "<td colspan=\"7\">店铺：<a href=\"#\">红豆豆的小屋</a>卖家<a href=\"#\">taobao豆豆</a><img src=\"../img/homework/taobao_relation.jpg\" alt=\"taobao_relation\" /></td>";
	shoppingcar.children[1].appendChild(store);
	store.className = "ptb5";
	var commodity = document.createElement("tr");
	commodity.innerHTML = "<td class=\"tac\"><input type=\"checkbox\" onclick=\"totalPrice();points();\" /></td>";
	commodity.innerHTML += "<td><div><img class=\"fl\" src=\"../img/homework/taobao_cart_04.jpg\" alt=\"taobao_cart_04\" /></div><div class=\"fl w350\"><p><a href=\"#\">相宜促销专供 大S推荐 最好用的LilyBell化妆棉</a></p><p>保障：<img src=\"../img/homework/taobao_icon_01.jpg\" alt=\"taobao_icon_01\" /></p></div></td>";
	commodity.innerHTML += "<td class=\"tac fw\">12</td><td class=\"tac\">12.00</td>";
	commodity.innerHTML += "<td class=\"tac\"><img class=\"cp\" src=\"../img/homework/taobao_minus.jpg\" alt=\"taobao_minus\" onclick=\"minusCommodity(this)\" /><input type=\"text\" size=\"1\" value=\"1\" onblur=\"subPrice(this);totalPrice();points();\" /><img class=\"cp\" src=\"../img/homework/taobao_adding.jpg\" alt=\"taobao_adding\" onclick=\"addCommodity(this)\" /></td>";
	commodity.innerHTML += "<td class=\"orange fs14 fw tac\">24</td><td class=\"tac\"><a href=\"#\" onclick=\"delCommodity(this)\">删除</a></td>";
	shoppingcar.children[1].appendChild(commodity);
	commodity.className = "bg ptb5";
}